/**
 * Copyright &copy; 2015-2020 <a href="http://www.easyhis.com/">easyhis</a> All rights reserved.
 */
package com.easy.modules.iim.service.impl;

import com.easy.common.utils.StringUtils;
import com.easy.core.persistence.Page;
import com.easy.core.service.impl.BaseServiceImpl;
import com.easy.modules.iim.entity.Mail;
import com.easy.modules.iim.entity.MailBox;
import com.easy.modules.iim.entity.MailCompose;
import com.easy.modules.iim.mapper.MailBoxMapper;
import com.easy.modules.iim.mapper.MailComposeMapper;
import com.easy.modules.iim.mapper.MailMapper;
import com.easy.modules.iim.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * 发件箱Service
 * @author easy
 * @version 2015-11-15
 */
@Service
@Transactional(readOnly = true)
public class MailServiceImpl extends BaseServiceImpl implements MailService {

	/**
	 * 持久层对象
	 */
	@Autowired
	private MailMapper mapper;
	@Autowired
	private MailBoxMapper mailBoxMapper;
	@Autowired
	private MailComposeMapper mailComposeMapper;
	

	

	@Transactional(readOnly = false)
	public void saveOnlyMain(Mail mail) {
		if (mail.getIsNewRecord()){
			mapper.insert(mail);
		}else{
			mapper.update(mail);
		}

		for (MailBox mailBox : mail.getMailBoxList()){
			if (mailBox.getId() == null){
				continue;
			}
			if (MailBox.DEL_FLAG_NORMAL.equals(mailBox.getDelFlag())){
				if (StringUtils.isBlank(mailBox.getId())){
					mailBox.setMail(mail);
					mailBox.preInsert();
					mailBoxMapper.insert(mailBox);
				}else{
					mailBox.preUpdate();
					mailBoxMapper.update(mailBox);
				}
			}else{
				mailBoxMapper.delete(mailBox);
			}
		}
		for (MailCompose mailCompose : mail.getMailComposeList()){
			if (mailCompose.getId() == null){
				continue;
			}
			if (MailCompose.DEL_FLAG_NORMAL.equals(mailCompose.getDelFlag())){
				if (StringUtils.isBlank(mailCompose.getId())){
					mailCompose.setMail(mail);
					mailCompose.preInsert();
					mailComposeMapper.insert(mailCompose);
				}else{
					mailCompose.preUpdate();
					mailComposeMapper.update(mailCompose);
				}
			}else{
				mailComposeMapper.delete(mailCompose);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(Mail mail) {
		mapper.delete(mail);
		mailBoxMapper.delete(new MailBox(mail));
		mailComposeMapper.delete(new MailCompose(mail));
	}


	/**
	 * 获取单条数据
	 * @param id
	 * @return
	 */
	public Mail get(String id) {
		Mail mail = mapper.get(id);
		mail.setMailBoxList(mailBoxMapper.findList(new MailBox(mail)));
		mail.setMailComposeList(mailComposeMapper.findList(new MailCompose(mail)));
		return mail;
	}

	/**
	 * 获取单条数据
	 * @param entity
	 * @return
	 */
	public Mail get(Mail entity) {
		return mapper.get(entity);
	}

	/**
	 * 查询列表数据
	 * @param entity
	 * @return
	 */
	public List<Mail> findList(Mail entity) {
		dataRuleFilter(entity);
		return mapper.findList(entity);
	}

	/**
	 * 查询分页数据
	 * @param page 分页对象
	 * @param entity
	 * @return
	 */
	public Page<Mail> findPage(Page<Mail> page, Mail entity) {
		dataRuleFilter(entity);
		entity.setPage(page);
		page.setList(mapper.findList(entity));
		return page;
	}

	/**
	 * 保存数据（插入或更新）
	 * @param mail
	 */
	@Transactional(readOnly = false)
	public void save(Mail mail) {
		if (mail.getIsNewRecord()){
			mapper.insert(mail);
		}else{
			mapper.update(mail);
		}

		for (MailBox mailBox : mail.getMailBoxList()){
			if (mailBox.getId() == null){
				continue;
			}
			if (MailBox.DEL_FLAG_NORMAL == mailBox.getDelFlag()){
				if (StringUtils.isBlank(mailBox.getId())){
					mailBox.setMail(mail);
					mailBox.preInsert();
					mailBoxMapper.insert(mailBox);
				}else{
					mailBox.preUpdate();
					mailBoxMapper.update(mailBox);
				}
			}else{
				mailBoxMapper.delete(mailBox);
			}
		}
		for (MailCompose mailCompose : mail.getMailComposeList()){
			if (mailCompose.getId() == null){
				continue;
			}
			if (MailCompose.DEL_FLAG_NORMAL == mailCompose.getDelFlag()){
				if (StringUtils.isBlank(mailCompose.getId())){
					mailCompose.setMail(mail);
					mailCompose.preInsert();
					mailComposeMapper.insert(mailCompose);
				}else{
					mailCompose.preUpdate();
					mailComposeMapper.update(mailCompose);
				}
			}else{
				mailComposeMapper.delete(mailCompose);
			}
		}
	}


	/**
	 * 删除全部数据
	 * @param mails
	 */
	@Transactional(readOnly = false)
	public void deleteAll(Collection<Mail> mails) {
		for (Mail mail : mails) {
			mapper.delete(mail);
		}
	}

	/**
	 * 删除全部数据
	 * @param mails
	 */
	@Transactional(readOnly = false)
	public void deleteAllByLogic(Collection<Mail> mails) {
		for (Mail mail : mails) {
			mapper.deleteLogic(mail);
		}
	}


	/**
	 * 获取单条数据
	 * @param propertyName, value
	 * @return
	 */
	public Mail findUniqueByProperty(String propertyName, Object value){
		return mapper.findUniqueByProperty(propertyName, value);
	}

	/**
	 * 动态sql
	 * @param sql
	 */

	public List<Object> executeSelectSql(String sql){
		return mapper.execSelectSql(sql);
	}

	@Transactional(readOnly = false)
	public void executeInsertSql(String sql){
		mapper.execInsertSql(sql);
	}

	@Transactional(readOnly = false)
	public void executeUpdateSql(String sql){
		mapper.execUpdateSql(sql);
	}

	@Transactional(readOnly = false)
	public void executeDeleteSql(String sql){
		mapper.execDeleteSql(sql);
	}

}